package calendar;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;

/* this class provides a panel to display event details */
class EventPanel extends CalendarPanel {

  private static final long serialVersionUID = 348394839L;

  JLabel title;
  JLabel descTitle;
  JLabel desc;
  JLabel type;
  JLabel startDate;
  JLabel endDate;
  JLabel membersTitle;
  ArrayList members;
  ArrayList memberLabels;
  String typeText;
  JButton deleteEventButton;
  int currentUserId;

  public EventPanel()
  {
    currentUserId = 0;
    memberLabels = new ArrayList();
    title = new JLabel( " Event Details:" );

    setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) );

    add( title );
  }

  /* show the event provided */
  public void showEvent( Event event, int currentUserId )
  {
    this.currentUserId = currentUserId;

    this.removeAll();

    if ( event.getType() == 1 ) typeText = "Private";
    else if ( event.getType() == 2 ) typeText = "Public";
    else if ( event.getType() == 3 ) typeText = "Group";

    descTitle = new JLabel ( " Event Description" );
    if ( event.getType() == 3 && !event.isParticipant( currentUserId ) )
    {
      desc = new JLabel( "    [hidden]" );
    }
    else desc = new JLabel( "    " + event.getDesc() );
    type = new JLabel( " Event Type:      " + typeText );
    startDate = new JLabel( " Start Date:      " + event.getStart().toString() );
    endDate = new JLabel( " End Date:        " + event.getEnd().toString() );
    membersTitle = new JLabel( " Event Member(s):" );
    members = event.getUsers();

    add( title );
    add ( Box.createVerticalStrut( 10 ) );
    add ( descTitle );
    add ( desc );
    add ( Box.createVerticalStrut( 10 ) );
    add ( type );
    add ( Box.createVerticalStrut( 10 ) );
    add ( startDate );
    add ( endDate );
    add ( Box.createVerticalStrut( 10 ) );
    add ( membersTitle );

    if ( members == null || members.isEmpty() )
    {
      members = new ArrayList();
      members.add( event.getOwner() );
    }

    String label = new String();
    label = "<html>&nbsp;&nbsp;&nbsp;&nbsp;";

    if ( event.getType() == 3 && !event.isParticipant( currentUserId ) )
      label += "[hidden]";
    else
    {
      Iterator itr = members.iterator();
      while ( itr.hasNext() )
        {
        User u = (User)itr.next();
        label = label + u.getName();
        if ( u.getId() == event.getOwnerId() ) label = label + " (Owner)";
        if ( itr.hasNext() ) label = label + ", ";
      }
    }

    label += "</html>";
    JLabel userLabel = new JLabel();
    userLabel.setText( label );
    add( userLabel );

    if ( event.getOwnerId() == currentUserId )
    {
      deleteEventButton = new JButton( "Delete Event" );
      deleteEventButton.addActionListener( this );

      Dimension deleteEventButtonSize = new Dimension( 150, 20 );
      deleteEventButton.setMaximumSize(deleteEventButtonSize);
      deleteEventButton.setMinimumSize(deleteEventButtonSize);
      deleteEventButton.setPreferredSize(deleteEventButtonSize);

      add ( deleteEventButton );
    }

    updateUI();
  }

  public void addTitle()
  {
    add( title );
  }

  public void actionPerformed(ActionEvent e) 
  {
    int eventId = -1;

    if ( e.getSource() == deleteEventButton )
    {
      eventId = CalEvent.DELETE_EVENT;
    }
    else return;

    CalEvent calEvent = new CalEvent( this, eventId );
    fireCalEvent( calEvent );
  }
}
